package com.example.feifei.view.lear;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.feifei.bean.PieChatBean;

import java.util.List;

/**
 * Created by feifei on 2018/5/30.
 */

public class DrawView extends View{

    private Paint mPaint;
    //饼状图数据
    private List<PieChatBean> listDatas;
    //饼状图宽高
    private int mWidth,mHeight;
    //饼状图初始角度
    private float mStartAngle = 0;
    // 颜色表 (注意: 此处定义颜色使用的是ARGB，带Alpha通道的)
    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};


    public DrawView(Context context) {
        super(context);
        initPaint();
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint(){
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(null == listDatas){
            return;
        }
        float currentStartAngle = mStartAngle;
        canvas.translate(mWidth/2,mHeight/2);
        float r = (float) (Math.min(mWidth,mHeight)/2*0.8);
        RectF rectF = new RectF(-r,-r,r,r);
//        canvas.drawArc(rectF,currentStartAngle,bean.getPercentage(),true,mPaint);
        for(int i = 0;i<listDatas.size();i++){
            PieChatBean bean = listDatas.get(i);
            mPaint.setColor(mColors[i%mColors.length]);
            canvas.drawArc(rectF,currentStartAngle,bean.getAngle(),true,mPaint);
            currentStartAngle+=bean.getAngle();
        }
    }
    // 设置起始角度
    public void setStartAngle(int mStartAngle) {
        this.mStartAngle = mStartAngle;
        invalidate();   // 刷新
    }
    /**
     * 设置圆饼数据
     */
    public void setPieData(List<PieChatBean> datas){
        this.listDatas = datas;
        initData(listDatas);
        invalidate();
    }

    private void initData(List<PieChatBean> datas){
        if(null == datas || datas.size() == 0){
            return;
        }
        float sumValue = 0;
        for(int i = 0;i< datas.size();i++){
            PieChatBean bean = datas.get(i);
            sumValue+=bean.getNummber();
        }

        float sumAngle = 0;
        for (int i = 0; i < datas.size(); i++) {
            PieChatBean pie = datas.get(i);

            float percentage = pie.getNummber() / sumValue;   // 百分比
            float angle = percentage * 360;                 // 对应的角度

            pie.setPercentage(percentage);                  // 记录百分比
            pie.setAngle(angle);                            // 记录角度大小
            sumAngle += angle;
        }
    }

}
